package com.yihuier.yihuier.Mapper.UserManager;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface MessageMapper {

    @Update("UPDATE bz_message SET message_read_status = 1 WHERE message_id = #{message_id} AND message_status = 1; ")
    int updateMessageReadStatus(@Param("message_id") int message_id);

    @Update("UPDATE bz_message SET message_status = 0 WHERE message_id = #{message_id}; ")
    int deleteMessageReadStatus(@Param("message_id") int message_id);

    @Select("<script>" +
            " SELECT message_id,message_content,message_datetime,message_read_status" +
            " FROM" +
            " bz_message" +
            " WHERE" +
            " message_status = 1 AND" +
            " message_to_id = #{user_id} AND" +
            " message_from_id IN" +
            " <foreach item='item' index='index' collection='managerIds' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " ORDER BY message_datetime DESC" +
            " </script> ")
    List<Map<String,Object>> selectMessages(@Param("user_id") int user_id, @Param("managerIds")int [] managerIds);

    @Insert("INSERT into bz_message(message_from_id,message_to_id,message_content,message_datetime) VALUES(#{message_from_id},#{message_to_id},#{message_content},#{message_datetime})")
    int insertMessage(@Param("message_from_id") int message_from_id,@Param("message_to_id") int message_to_id,@Param("message_content") String message_content,@Param("message_datetime") String message_datetime);

    /**
     * 管理员查看自己所发的所有信息
     * */
    @Select("SELECT message_id,message_to_id,message_content,message_datetime,message_read_status FROM bz_message WHERE message_from_id = #{manager_id} AND message_status = 1")
    List<Map<String,Object>> selectAllMessageFromManagerSelf(@Param("manager_id") int manager_id);

}
